import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/books.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/widgets/dialogs/addToFolderDialog.dart';
import 'package:ichinesereader/widgets/dialogs/addToPlaylistDialog.dart';
import 'package:ichinesereader/widgets/dialogs/simpleAlertDialog.dart';
import 'package:ichinesereader/widgets/dialogs/searchDialog.dart';
import 'package:ichinesereader/widgets/dialogs/simpleSearchDialog.dart';
import '../../pages/teacher/homeworkStepper.dart';
import '../../themes/appTheme.dart';

// ignore: must_be_immutable
class FloatingActionButtonAddBookToFolder extends StatelessWidget {
  FloatingActionButtonAddBookToFolder({
    Key? key,
    required this.readingType,
  }) : super(key: key);

  NanhaiBookReadingTypes readingType;
  String readingTypeFrom = '';
  final BooksController _controller = Get.find();
  final box = Hive.box('appconfig');

  @override
  Widget build(BuildContext context) {
    bool isTeacher = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Teacher;
    bool isStudentOrIndividual =
        box.get(CacheManagerKey['nanhairole'].toString()) ==
                Constants.NanhaiRole_Individual ||
            box.get(CacheManagerKey['nanhairole'].toString()) ==
                Constants.NanhaiRole_Student;

    return Column(
      mainAxisAlignment: MainAxisAlignment.end,
      children: <Widget>[
        //if (this.readingType != NanhaiBookReadingTypes.assignment )
        if (this.readingType == NanhaiBookReadingTypes.library ||
            this.readingType == NanhaiBookReadingTypes.leveled)
          SizedBox(
            width: 45.0,
            height: 45.0,
            child: FloatingActionButton(
              backgroundColor: Get.isDarkMode
                  ? Theme.of(context).primaryColorDark
                  : AppTheme.colors.orangeBtn,
              foregroundColor:
                  Get.isDarkMode ? Colors.black : AppTheme.colors.lightOrange,
              heroTag: Text('SearchBooks888888'.tr),
              tooltip: 'Search Books'.tr,
              onPressed: () {
                List<String> bookids = [];
                if (this.readingType == NanhaiBookReadingTypes.leveled) {
                  readingTypeFrom = "leveled";
                  _controller.selectedLeveledReadingBookItems.forEach(
                    (element) {
                      if (!bookids.contains(element.bookId))
                        bookids.add(element.bookId.toString());
                    },
                  );
                }

                if (this.readingType == NanhaiBookReadingTypes.library) {
                  readingTypeFrom = "library";
                  _controller.selectedLibraryReadingBookItems.forEach(
                    (element) {
                      if (!bookids.contains(element.bookId))
                        bookids.add(element.bookId.toString());
                    },
                  );
                }

                if (this.readingType == NanhaiBookReadingTypes.favorite) {
                  readingTypeFrom = "favorite";
                  _controller.selectedFavoriteReadingBookItems.forEach(
                    (element) {
                      if (!bookids.contains(element.bookId))
                        bookids.add(element.bookId.toString());
                    },
                  );
                }

                if (this.readingType == NanhaiBookReadingTypes.assignment) {
                  readingTypeFrom = "assignment";
                  _controller.selectedAssignedBookItems.forEach(
                    (element) {
                      if (!bookids.contains(element.bookId))
                        bookids.add(element.bookId.toString());
                    },
                  );
                }

                if (this.readingType == NanhaiBookReadingTypes.evaluation) {
                  readingTypeFrom = "evaluation";
                  _controller.selectedEvaluationBookItems.forEach(
                    (element) {
                      if (!bookids.contains(element.bookId))
                        bookids.add(element.bookId.toString());
                    },
                  );
                }

                if (this.readingType == NanhaiBookReadingTypes.theme) {
                  readingTypeFrom = "theme";
                  _controller.selectedThemedBookItems.forEach(
                    (element) {
                      if (!bookids.contains(element.bookId))
                        bookids.add(element.bookId.toString());
                    },
                  );
                }

                if (this.readingType == NanhaiBookReadingTypes.textbook) {
                  readingTypeFrom = "textbook";
                  _controller.selectedTextBookItems.forEach(
                    (element) {
                      if (!bookids.contains(element.bookId))
                        bookids.add(element.bookId.toString());
                    },
                  );
                }
                showDialog(
                    context: context,
                    builder: (BuildContext context) {
                      return SearchDialog(
                          readingType: this.readingType,
                          title: 'Search Books'.tr,
                          selected: bookids,
                          message: '',
                          readingTypeFrom: readingTypeFrom);
                    });
              },
              child: Icon(
                Icons.search,
                color:
                    Get.isDarkMode ? Colors.black : AppTheme.colors.lightOrange,
                size: 24,
              ),
              elevation: 12,
              splashColor: Colors.grey,
            ),
          ),
        if (this.readingType == NanhaiBookReadingTypes.theme && isTeacher)
          SizedBox(
            width: 45.0,
            height: 45.0,
            child: FloatingActionButton(
              backgroundColor: Get.isDarkMode
                  ? Theme.of(context).primaryColorDark
                  : AppTheme.colors.orangeBtn,
              foregroundColor:
                  Get.isDarkMode ? Colors.black : AppTheme.colors.lightOrange,
              heroTag: Text('SimpleSearchBooks8899'.tr),
              tooltip: 'Search Books'.tr,
              onPressed: () {
                showDialog(
                    context: context,
                    builder: (BuildContext context) {
                      return SimpleBookSearchDialogue(
                          readingType: this.readingType);
                    });
              },
              child: Icon(
                Icons.search,
                color:
                    Get.isDarkMode ? Colors.black : AppTheme.colors.lightOrange,
                size: 24,
              ),
              elevation: 12,
              splashColor: Colors.grey,
            ),
          ),
        SizedBox(height: 9.0),
        SizedBox(
          width: 45.0,
          height: 45.0,
          child: FloatingActionButton(
            backgroundColor: Get.isDarkMode
                ? Theme.of(context).primaryColorDark
                : AppTheme.colors.addBtn,
            foregroundColor: Get.isDarkMode
                ? Colors.black
                : Theme.of(context).primaryColorLight,
            heroTag: Text('addbooktofolder'.tr),
            tooltip: isTeacher ? 'Add to Lesson Plan'.tr : 'Add to folder'.tr,
            onPressed: () {
              _controller.fetchBookAllFolders();
              List<String> bookids = [];
              if (_controller.selectedLeveledReadingBookItems.length > 0 ||
                  _controller.selectedFavoriteReadingBookItems.length > 0 ||
                  _controller.selectedLibraryReadingBookItems.length > 0 ||
                  _controller.selectedAssignedBookItems.length > 0 ||
                  _controller.selectedEvaluationBookItems.length > 0 ||
                  _controller.selectedSearchedBookItems.length > 0 ||
                  _controller.selectedThemedBookItems.length > 0) {
                if (this.readingType == NanhaiBookReadingTypes.leveled) {
                  readingTypeFrom = "leveled";
                  _controller.selectedLeveledReadingBookItems.forEach(
                    (element) {
                      if (!bookids.contains(element.bookId))
                        bookids.add(element.bookId.toString());
                    },
                  );
                }

                if (this.readingType == NanhaiBookReadingTypes.library) {
                  readingTypeFrom = "library";
                  _controller.selectedLibraryReadingBookItems.forEach(
                    (element) {
                      if (!bookids.contains(element.bookId))
                        bookids.add(element.bookId.toString());
                    },
                  );
                }

                if (this.readingType == NanhaiBookReadingTypes.favorite) {
                  readingTypeFrom = "favorite";
                  _controller.selectedFavoriteReadingBookItems.forEach(
                    (element) {
                      if (!bookids.contains(element.bookId))
                        bookids.add(element.bookId.toString());
                    },
                  );
                }

                if (this.readingType == NanhaiBookReadingTypes.assignment) {
                  readingTypeFrom = "assignment";
                  _controller.selectedAssignedBookItems.forEach(
                    (element) {
                      if (!bookids.contains(element.bookId))
                        bookids.add(element.bookId.toString());
                    },
                  );
                }

                if (this.readingType == NanhaiBookReadingTypes.evaluation) {
                  readingTypeFrom = "evaluation";
                  _controller.selectedEvaluationBookItems.forEach(
                    (element) {
                      if (!bookids.contains(element.bookId))
                        bookids.add(element.bookId.toString());
                    },
                  );
                }
                if (this.readingType == NanhaiBookReadingTypes.theme) {
                  readingTypeFrom = "theme";
                  _controller.selectedThemedBookItems.forEach(
                    (element) {
                      if (!bookids.contains(element.bookId))
                        bookids.add(element.bookId.toString());
                    },
                  );
                }

                if (this.readingType == NanhaiBookReadingTypes.search) {
                  readingTypeFrom = "search";
                  _controller.selectedSearchedBookItems.forEach(
                    (element) {
                      if (!bookids.contains(element.bookId))
                        bookids.add(element.bookId.toString());
                    },
                  );
                }

                if (bookids.length > 0)
                  showGeneralDialog(
                      context: context,
                      transitionDuration: Duration(milliseconds: 400),
                      pageBuilder: (bc, ania, anis) {
                        return AddToFolderDialog(
                            readingType: this.readingType,
                            title: isTeacher
                                ? 'Add to Lesson Plan'.tr
                                : 'Add to folder'.tr,
                            selected: bookids,
                            message: '',
                            readingTypeFrom: readingTypeFrom);
                      });
              } else {
                showGeneralDialog(
                    context: context,
                    transitionDuration: Duration(milliseconds: 400),
                    pageBuilder: (bc, ania, anis) {
                      return SimpleAlertDialog(
                          title: 'No Book'.tr,
                          message:
                              'Please select at least one book to ADD!'.tr);
                    });
              }
            },
            child: Icon(
              Icons.add,
              color:
                  Get.isDarkMode ? Colors.black : Get.theme.primaryColorLight,
              size: 24,
            ),
            elevation: 12,
            splashColor: Colors.grey,
          ),
        ),
        SizedBox(height: 9.0),
        if (isStudentOrIndividual)
          SizedBox(
            width: 45.0,
            height: 45.0,
            child: FloatingActionButton(
              backgroundColor: Get.isDarkMode
                  ? Theme.of(context).primaryColorDark
                  : AppTheme.colors.addBtn,
              foregroundColor: Get.isDarkMode
                  ? Colors.black
                  : Theme.of(context).primaryColorLight,
              heroTag: Text('addbooktoAudioList8899'.tr),
              tooltip: 'Add Books to Audio Playlist'.tr,
              onPressed: () {
                List<String> bookids = [];
                if (_controller.selectedLeveledReadingBookItems.length > 0 ||
                    _controller.selectedFavoriteReadingBookItems.length > 0 ||
                    _controller.selectedLibraryReadingBookItems.length > 0 ||
                    _controller.selectedAssignedBookItems.length > 0 ||
                    _controller.selectedEvaluationBookItems.length > 0 ||
                    _controller.selectedSearchedBookItems.length > 0 ||
                    _controller.selectedThemedBookItems.length > 0) {
                  if (this.readingType == NanhaiBookReadingTypes.leveled) {
                    readingTypeFrom = "leveled";
                    _controller.selectedLeveledReadingBookItems.forEach(
                      (element) {
                        if (!bookids.contains(element.bookId))
                          bookids.add(element.bookId.toString());
                      },
                    );
                  }

                  if (this.readingType == NanhaiBookReadingTypes.library) {
                    readingTypeFrom = "library";
                    _controller.selectedLibraryReadingBookItems.forEach(
                      (element) {
                        if (!bookids.contains(element.bookId))
                          bookids.add(element.bookId.toString());
                      },
                    );
                  }

                  if (this.readingType == NanhaiBookReadingTypes.favorite) {
                    readingTypeFrom = "favorite";
                    _controller.selectedFavoriteReadingBookItems.forEach(
                      (element) {
                        if (!bookids.contains(element.bookId))
                          bookids.add(element.bookId.toString());
                      },
                    );
                  }

                  if (this.readingType == NanhaiBookReadingTypes.assignment) {
                    readingTypeFrom = "assignment";
                    _controller.selectedAssignedBookItems.forEach(
                      (element) {
                        if (!bookids.contains(element.bookId))
                          bookids.add(element.bookId.toString());
                      },
                    );
                  }
                  if (this.readingType == NanhaiBookReadingTypes.evaluation) {
                    readingTypeFrom = "assignment";
                    _controller.selectedEvaluationBookItems.forEach(
                      (element) {
                        if (!bookids.contains(element.bookId))
                          bookids.add(element.bookId.toString());
                      },
                    );
                  }
                  if (this.readingType == NanhaiBookReadingTypes.theme) {
                    readingTypeFrom = "theme";
                    _controller.selectedThemedBookItems.forEach(
                      (element) {
                        if (!bookids.contains(element.bookId))
                          bookids.add(element.bookId.toString());
                      },
                    );
                  }

                  if (this.readingType == NanhaiBookReadingTypes.search) {
                    readingTypeFrom = "search";
                    _controller.selectedSearchedBookItems.forEach(
                      (element) {
                        if (!bookids.contains(element.bookId))
                          bookids.add(element.bookId.toString());
                      },
                    );
                  }
                  if (bookids.length > 0)
                    showGeneralDialog(
                        context: context,
                        transitionDuration: Duration(milliseconds: 400),
                        pageBuilder: (bc, ania, anis) {
                          return AddToPlaylistDialog(
                              title: '',
                              message: 'Add Books to Audio Playlist'.tr,
                              selected: bookids,
                              readingTypeFrom: readingTypeFrom);
                        });
                } else {
                  showGeneralDialog(
                      context: context,
                      transitionDuration: Duration(milliseconds: 400),
                      pageBuilder: (bc, ania, anis) {
                        return SimpleAlertDialog(
                            title: 'No Book'.tr,
                            message:
                                'Please select at least one book to ADD to audio palylist!'
                                    .tr);
                      });
                }
              },
              child: Icon(
                Icons.audiotrack,
                color:
                    Get.isDarkMode ? Colors.black : Get.theme.primaryColorLight,
                size: 24,
              ),
              elevation: 12,
              splashColor: Colors.grey,
            ),
          ),
        if (isTeacher)
          SizedBox(
            width: 45.0,
            height: 45.0,
            child: FloatingActionButton(
              backgroundColor: Get.isDarkMode
                  ? Theme.of(context).primaryColorDark
                  : AppTheme.colors.pinkBtn,
              foregroundColor:
                  Get.isDarkMode ? Colors.black : AppTheme.colors.lightPink,
              heroTag: Text('createhomework889900099'),
              tooltip: 'Create Homework'.tr,
              onPressed: () {
                if (_controller.selectedLibraryReadingBookItems.length > 0 ||
                    _controller.selectedThemedBookItems.length > 0) {
                  if (this.readingType == NanhaiBookReadingTypes.theme) {
                    Get.to(() => HomeworkSteps(
                          isNew: true,
                          books: _controller.selectedThemedBookItems,
                        ));
                  } else if (this.readingType ==
                      NanhaiBookReadingTypes.assignment) {
                    Get.to(() => HomeworkSteps(
                          isNew: true,
                          books: _controller.selectedLibraryReadingBookItems,
                        ));
                  }
                  Get.to(() => HomeworkSteps(
                        isNew: true,
                        books: _controller.selectedLibraryReadingBookItems,
                      ));
                } else {
                  showGeneralDialog(
                      context: context,
                      transitionDuration: Duration(milliseconds: 400),
                      pageBuilder: (bc, ania, anis) {
                        return SimpleAlertDialog(
                            title: 'No Book'.tr,
                            message:
                                'Please select at least one book to ASSIGN!'
                                    .tr);
                      });
                }
              },
              child: Icon(
                Icons.assignment,
                color:
                    Get.isDarkMode ? Colors.black : AppTheme.colors.lightPink,
                size: 24,
              ),
              elevation: 12,
              splashColor: Colors.grey,
            ),
          ),
      ],
    );
  }
}
